package com.emergency.db.service.impl.work;

import com.emergency.base.domain.Order;
import com.emergency.common.util.CommonUtils;
import com.emergency.common.util.IdWorker;
import com.emergency.db.dao.work.OrderDao;
import com.emergency.db.service.impl.staff.StaffServiceImpl;
import com.emergency.db.service.work.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {
    private final static Logger logger = LoggerFactory.getLogger(StaffServiceImpl.class);

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private IdWorker idWorker;

    //保存
    public Long save(Order order) {
        if (CommonUtils.isNullOrEmpty(order.getId())) {
            order.setId(idWorker.nextId());
            order.setCreateTime(new Date());
        }
        orderDao.save(order);
        return order.getId();
    }

    //批量保存
    public void saveAll(List<Order> orderList) {
        orderList.forEach(order -> {
            if (CommonUtils.isNullOrEmpty(order.getId())) {
                order.setId(idWorker.nextId());
                order.setCreateTime(new Date());
            }
            orderDao.save(order);
        });
    }

    //查询单项
    public Order findById(Long id) {
        Order order = orderDao.findById(id).orElse(null);
        if (CommonUtils.isNullOrEmpty(order)) {
            logger.info("此信息为空！");
            return order;
        }
        return order;
    }

    //删除
    public Long delete(Long id) {
        orderDao.deleteById(id);
        return id;
    }

    //查询全部
    @Override
    public List<Order> findAll() {
        return orderDao.findAll();
    }

    //分页查询
    @Override
    public Page<Order> findList(Map searchMap, int page, int size) {
        Specification<Order> specification = createSpecification(searchMap);
        Sort sort = Sort.by(Sort.Direction.ASC, "id");
        PageRequest pageRequest = PageRequest.of(page - 1, size, sort);
        return orderDao.findAll(specification, pageRequest);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<Order> createSpecification(Map searchMap) {
        return new Specification<Order>() {

            @Override
            public Predicate toPredicate(Root<Order> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<Predicate>();

                // 名称
                if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                    predicateList.add(criteriaBuilder.like(root.get("name").as(String.class), "%" + (String) searchMap.get("name") + "%"));
                }
                //类型
                if (searchMap.get("type") != null && !"".equals(searchMap.get("type"))) {
                    predicateList.add(criteriaBuilder.like(root.get("type").as(String.class), "%" + (String) searchMap.get("type") + "%"));
                }
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }
}
